package com.aierjun.ani.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aierjun.ani.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ani_aierJun on 2017/9/30.
 */


public class ImageShowManageView extends LinearLayout {
    private Context mContext;
    private final String TAG = "ImageShowManageView";
    private int itemNum = 4; //一排的数量
    private int index = 0;
    private OnDelItemClickListener onDelItemClickListener; // 删除onclick
    private List<String> list = new ArrayList<>(); // data数据
    private List<LinearLayout> linearLayoutList = new ArrayList<>();//行
    private OnAddClickListener onClickListener; // 图片添加onclick
    private List<ImageView> imageViewList = new ArrayList<>();// imageview list （no delimageview）
    private boolean visible = true; //是否显示最后一个添加image
    private int delVisible = VISIBLE; //是否显示删除图片和激活删除功能
    private LinearLayout linearLayoutNowC; //当前操作的行的LinearLayout
    private int TagNUM = 0;
    private OnDelItemTAGClickListener onDelItemTAGClickListener;
    private OnImageOnClickListenter onImageOnClickListenter;

    public ImageShowManageView(Context context) {
        this(context, null);
    }

    public ImageShowManageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageShowManageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        linearLayoutList.clear();
        imageViewList.clear();
        this.setOrientation(VERTICAL);
        return;
    }


    /*
    *
    *  创建imgageview和delimageview
    * @param parent 添加的父类layout
    * @param p 区别是defualt还是setData
    *
    * */
    public ImageView createImggeView(LinearLayout parent, int p) {
        //第二层 分一份
        RelativeLayout relativeLayout = new RelativeLayout(mContext);
        LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        relativeLayout.setLayoutParams(params);
        // 第三层 一份里的 加载图片
        ImageView imageView = new ImageView(mContext);
        RelativeLayout.LayoutParams relativeLayoutImg = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 150);
        imageView.setLayoutParams(relativeLayoutImg);
        imageView.setPadding(5, 10, 5, 0);
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.expressbg));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setTag(index);
        // 第三层 一份里的 删除图片
        ImageView imageViewdel = new ImageView(mContext);
        RelativeLayout.LayoutParams relativeLayoutdel = new RelativeLayout.LayoutParams(80, 80);
        relativeLayoutdel.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        imageViewdel.setLayoutParams(relativeLayoutdel);
        imageViewdel.setImageDrawable(getResources().getDrawable(R.drawable.untitled));
        imageViewdel.setTag(index);
        imageViewdel.setVisibility(GONE);

        //defualt
        if (p == 0) {
            if (onClickListener != null)
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onClickListener.onClick(view, (Integer) view.getTag());
                    }
                });
        } else {              //setdata
            if (index != list.size()) {
                imageViewdel.setVisibility(delVisible);
                imageViewdel.setEnabled(true);
                imageViewdel.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ImageView v = (ImageView) view;
                        int t = (int) v.getTag();
                        removeImageView((ImageView) view);
                        List<String> stringList = getList();
                        if (onDelItemClickListener != null)
                            onDelItemClickListener.onClickDelItem(v, stringList);
                        if (onDelItemTAGClickListener != null)
                            onDelItemTAGClickListener.onClickDelItem(TagNUM,stringList);

                    }
                });
                if (onImageOnClickListenter != null)
                    imageView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onImageOnClickListenter.onImageClick(view, (Integer) view.getTag());
                        }
                    });

            } else {
                if (!visible)
                    return imageView;
                if (onClickListener != null)
                    imageView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onClickListener.onClick(view, (Integer) view.getTag());
                        }
                    });
                imageViewdel.setVisibility(GONE);
                imageViewdel.setEnabled(false);
            }
            index++;
        }
        //添加
        relativeLayout.addView(imageView, 0);
        relativeLayout.addView(imageViewdel, 1);
        parent.addView(relativeLayout);
        return imageView;
    }


    /*
   *
   *  是否显示最后的添加imageview
   * @param visible 是否显示
   *
   * */
    public ImageShowManageView showEndView(boolean visible) {
        this.visible = visible;
        return this;
    }

    /*
    *
    *  删除imageview
    * @param view 删除的view
    *
    * */
    private void removeImageView(ImageView view) {
        LinearLayout linearLayoutNow;
        RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
        for (int a = 0; a < linearLayoutList.size(); a++) {
            for (int b = 0; b < itemNum; b++) {
                if (linearLayoutList.get(a).getChildAt(b) == relativeLayout) {
                    linearLayoutNow = linearLayoutList.get(a);
                    linearLayoutList.get(a).removeView(linearLayoutList.get(a).getChildAt(b));
                    int tt = (int) relativeLayout.getChildAt(0).getTag();
                    imageViewList.remove(tt);
                    list.remove(tt);
                    for (int c = a + 1; c < linearLayoutList.size(); c++) {
                        View child = linearLayoutList.get(c).getChildAt(0);
                        if (child != null) {
                            linearLayoutList.get(c).removeView(child);
                            linearLayoutNow.addView(child);
                        }
                        linearLayoutNow = linearLayoutList.get(c);
                    }
                }
            }
        }
    }

    public List<String> getList() {
        for (int i = 0; i < imageViewList.size(); i++)
            imageViewList.get(i).setTag(i);
        index = (Integer) imageViewList.get(imageViewList.size() - 1).getTag() + 1;
        Log.d(TAG, list.toArray().toString());
        return list;
    }

    /*
    *
    *  显示defalut 的view
    *
    * */
    public ImageShowManageView defaultView() {
        LinearLayout linearLayout = addParent();
        imageViewList.add(createImggeView(linearLayout, 0));
        this.addView(linearLayout, 0);
        return this;
    }


    /*
    *
    *  图片数据
    * @param list 图片地址list
    * @param baseURL base地址
    *
    * */
    public ImageShowManageView setImgData(final List<String> lists, String baseURL) {
        list.clear();
        for (int i = 0; i < lists.size(); i++) //避免数据更改出现异常
            list.add(lists.get(i));
        linearLayoutList.clear();
        imageViewList.clear();
        removeAllViews();
        index = 0;

        //最外层 平均分itemNum分
        int num = list.size() / itemNum;
        for (int i = 0; i < num; i++) {
            LinearLayout linearLayout = addParent();
            for (int ii = 0; ii < itemNum; ii++) {
                imageViewList.add(createImggeView(linearLayout, 1));
            }
            this.addView(linearLayout, i);
        }
        if (list.size() % itemNum > 0) {
            linearLayoutNowC = addParent();
            for (int ii = 0; ii < list.size() % itemNum + 1; ii++) {
                imageViewList.add(createImggeView(linearLayoutNowC, 1));
            }
            this.addView(linearLayoutNowC, num);
        }
        if (list.size() % itemNum == 0) {
            linearLayoutNowC = addParent();
            if (visible == true) {
                ImageView imageView = createImggeView(linearLayoutNowC, 1);
                if (imageView != null)
                    imageViewList.add(imageView);
                this.addView(linearLayoutNowC, num);
            }
        }
        if (baseURL == null)
            baseURL = "";
        for (int i = 0; i < list.size(); i++) {
            ImageLoader.getInstance().displayImage(baseURL + list.get(i), imageViewList.get(i));
        }
        return this;
    }


    /*
    *
    *  添加图片数据
    * @param  path图片地址
    *
    * */
    public ImageShowManageView setAddViewStr(String path , String baseURL) {
        if (linearLayoutNowC == null) {
            linearLayoutList.clear();
            imageViewList.clear();
            removeAllViews();
            linearLayoutNowC = addParent();
            ImageView imageView = createImggeView(linearLayoutNowC, 1);
            if (imageView != null)
                imageViewList.add(imageView);
            this.addView(linearLayoutNowC, 0);
        }
        View viewSeat = linearLayoutNowC.getChildAt(linearLayoutNowC.getChildCount() - 1);
        if (viewSeat != null)
            linearLayoutNowC.removeView(viewSeat);
        ImageView view = createImggeView(linearLayoutNowC, 1);
        ImageView lastImg = imageViewList.get(imageViewList.size() - 1);
        int t = (int) view.getTag();
        view.setTag(lastImg.getTag());
        lastImg.setTag(t);
        imageViewList.remove(imageViewList.size() - 1);
        imageViewList.add(view);
        imageViewList.add(lastImg);

        ImageLoader.getInstance().displayImage(baseURL + path, view);
        Log.d(TAG, path);
        if (linearLayoutNowC.getChildCount() == itemNum) {
            linearLayoutNowC = addParent();
            this.addView(linearLayoutNowC);
        }
        if (viewSeat != null)
            linearLayoutNowC.addView(viewSeat);
        list.add(path);
        return this;
    }

    public interface OnDelItemClickListener {
        void onClickDelItem(View view, List<String> list);
    }
    public interface OnDelItemTAGClickListener {
        void onClickDelItem(int tag, List<String> list);
    }

    public interface OnImageOnClickListenter{
        void onImageClick(View view, int tag);
    }

    public ImageShowManageView setOnImageOnClickListenter(OnImageOnClickListenter onImageOnClickListenter){
        this.onImageOnClickListenter = onImageOnClickListenter;
        return this;
    }

    /*
    *
    *  添加删除回调
    * @param  onDelItemClickListener
    *
    * */
    public ImageShowManageView setOnDelItemClickListener(OnDelItemClickListener onDelItemClickListener) {
        this.onDelItemClickListener = onDelItemClickListener;
        return this;
    }

    /*
*
*  添加删除回调
* @param  onDelItemClickListener
* @param tag 当多个时用于区分的tag值
*
* */
    public ImageShowManageView setOnDelItemTAGClickListener(OnDelItemTAGClickListener onDelItemTAGClickListener, int tag) {
        this.onDelItemTAGClickListener = onDelItemTAGClickListener;
        this.TagNUM = tag;
        return this;
    }

    /*
    *
    *  添加点击添加回调
    * @param  OnAddClickListener
    *
    * */
    public ImageShowManageView setAddOnClickListener(OnAddClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public interface OnAddClickListener {
        void onClick(View view, int tag);
    }

    /*
    *
    *  添加父layout
    *
    * */
    private LinearLayout addParent() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(layoutParams1);
        linearLayout.setWeightSum(itemNum);
        linearLayout.setPadding(10, 5, 10, 5);
        linearLayoutList.add(linearLayout);
        return linearLayout;
    }

    /*
    *
    *  设置每行个数
    * @param  itemNum  个数
    *
    * */
    public ImageShowManageView setTtemNum(int itemNum) {
        this.itemNum = itemNum;
        return this;
    }

    /*
    *
    *  是否显示删除图标和激活删除功能
    * @param  visible  是否显示
    *
    * */
    public ImageShowManageView showDel(boolean visible) {
        delVisible = visible == true ? View.VISIBLE : View.GONE;
        return this;
    }

}
